#include<stdio.h>
#include<string.h>
#include<windows.h>
#include <conio.h>

#define N 15

// Global variables to store player's position
int x, y;
// The game map: 0 is empty space, 1 is wall, 3 is player, 4 is target/exit
int map[N][N] = {
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 0, 3, 1, 1, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
        {1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

// Function declarations
void print();   // To display the game map
void find();    // To locate the player
int move();     // To handle player movement
int Up();       // Move up
int Down();     // Move down
int Right();    // Move right
int Left();     // Move left

int main() {
    int flag = 0;
    

    // Main game loop
    while (1) {
        flag = move();
        if (flag == 1 || flag == -1)  // Exit loop if game ends (win or quit)
            return 0;
    }
    return 0;
}

// Locate the player's position on the map
void find() {
    int i = 0, j = 0;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (map[i][j] == 3) {  // Player is represented by 3
                x = i;  // Update player's x-coordinate
                y = j;  // Update player's y-coordinate
                break;
            }
        }
    }
}

// Handle player movement
int move() {
    int flag = 0;
    while (1) {
        flag = 0;
        find();  // Get current player position
        print(); // Refresh the display

        // Get player input for movement
        switch (getch()) { // getch() gets a character from the keyboard buffer
            case 'w':  // Move up
                flag = Up();
                break;
            case 'a':  // Move left
                flag = Left();
                break;
            case 's':  // Move down
                flag = Down();
                break;
            case 'd':  // Move right
                flag = Right();
                break;
            case 27:   // Escape key to exit
                return -1;
        }

        // Check if the game is won (reached exit marked by 4)
        if (flag == 1) {
            MessageBox(0, TEXT("GAME OVER!!! "), TEXT("YOU WIN"), MB_OK); // Display win message
            return 1;
        }
    }
}

// Move the player up
int Up() {
    if (x != 0) {  // Check if within bounds (not at top edge)
        if (map[x - 1][y] == 1) {  // Check if the target cell is a wall
            map[x - 1][y] = 3;  // Update player position
            map[x][y] = 1;      // Mark previous position as wall
        } else if (map[x - 1][y] == 4) {  // Check if reached target/exit
            return 1;
        }
    }
    return 0;
}

// Move the player down
int Down() {
    if (x != N - 1) {  // Check if within bounds (not at bottom edge)
        if (map[x + 1][y] == 1) {  // Check if target cell is a wall
            map[x + 1][y] = 3;  // Update player position
            map[x][y] = 1;      // Mark previous position as wall
        } else if (map[x + 1][y] == 4) {  // Check if reached target/exit
            return 1;
        }
    }
    return 0;
}

// Move the player right
int Right() {
    if (y != N - 1) {  // Check if within bounds (not at right edge)
        if (map[x][y + 1] == 1) {  // Check if target cell is a wall
            map[x][y + 1] = 3;  // Update player position
            map[x][y] = 1;      // Mark previous position as wall
        } else if (map[x][y + 1] == 4) {  // Check if reached target/exit
            return 1;
        }
    }
    return 0;
}

// Move the player left
int Left() {
    if (y != 0) {  // Check if within bounds (not at left edge)
        if (map[x][y - 1] == 1) {  // Check if target cell is a wall
            map[x][y - 1] = 3;  // Update player position
            map[x][y] = 1;      // Mark previous position as wall
        } else if (map[x][y - 1] == 4) {  // Check if reached target/exit
            return 1;
        }
    }
    return 0;
}

// Display the game map
void print() {
    int i = 0, j = 0;
    
    system("cls");  // Clear the console screen
    printf("WSAD MOVE\n");  // Display movement controls

    // Iterate through and print the map
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            if (map[i][j] == 0)
                printf("x");  // Wall 
            else if (map[i][j] == 1)
                printf(" "); // Empty space
            else if (map[i][j] == 3)
                printf("o");  // Player
            else if (map[i][j] == 4)
                printf("*");  // Target/Exit
        }
        printf("\n");
    }
}
